﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Etapa1.SofRender
{
    public class InputAssembler
    {
        VertexType[] vertices;
        int[] indexBuffer;
        int iterator;
        bool useIndexBuffer;

        public void SetData(int[] indexBuffer, VertexType[] vertices) 
        {
            this.vertices = vertices;
            this.indexBuffer = indexBuffer;
            iterator = 0;
            useIndexBuffer = true;
        }

        public void SetData(VertexType[] vertices)
        {
            this.vertices = vertices;            
            iterator = 0;
            useIndexBuffer = false;
        }

        public VertexType GetPoint()
        {
            if (useIndexBuffer)
            {
                if (iterator >= indexBuffer.Count())
                    return null;
                
                return vertices[indexBuffer[iterator++]];
                
            }
            else
            {
                if (iterator >= vertices.Count())
                    return null;
                
                return vertices[iterator++];
            }
        }

        public VertexType[] GetTriangle()
        {
            if (useIndexBuffer)
            {
                if (iterator >= indexBuffer.Count())
                    return null;

                VertexType[] vi = new VertexType[3];
                vi[0] = vertices[indexBuffer[iterator++]];
                vi[1] = vertices[indexBuffer[iterator++]];
                vi[2] = vertices[indexBuffer[iterator++]];

                return vi;
            }
            else
            {
                if (iterator >= vertices.Count())
                    return null;

                VertexType[] vi = new VertexType[3];
                vi[0] = vertices[iterator++];
                vi[1] = vertices[iterator++];
                vi[2] = vertices[iterator++];

                return vi;
            }
        }
    }
}
